package com.example.blue;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Locale;

public class DuizhiResActivity extends AppCompatActivity {
    public Integer predictLabel;
    public Integer predictScore;
    public String[] displayFea;
    public String displayFeaStr;

    public Integer predictLabelR;
    public Integer predictScoreR;
    public String[] displayFeaR;
    public String displayFeaStrR;

    private TextView textViewTitle;
    private TextView textView1;
    private TextView textView2;
    private TextView textView3;
    private TextView textView4;
    private TextView textView5;
    private TextView textView6;

    private TextView textViewTitleR;
    private TextView textView1R;
    private TextView textView2R;
    private TextView textView3R;
    private TextView textView4R;
    private TextView textView5R;
    private TextView textView6R;
    private LinearLayout linearLayoutLeft;
    private LinearLayout linearLayoutRight;

    private Integer conDeviceNum;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CrashHandler.getInstance().init(this);
        setContentView(R.layout.activity_duizhi_res);
        // 开启返回箭头
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(null);
        getSupportActionBar().setTitle("对指测试结果");

        textViewTitle=findViewById(R.id.test_hand);
        textView1 = findViewById(R.id.jiaosudu);
        textView2 = findViewById(R.id.dakaijiao);
        textView3 = findViewById(R.id.bihejiao);
        textView4 = findViewById(R.id.duzhichi);
        textView5 = findViewById(R.id.yucelabel);
        textView6 = findViewById(R.id.yucescore);
        textViewTitleR=findViewById(R.id.test_handR);
        textView1R = findViewById(R.id.jiaosuduR);
        textView2R = findViewById(R.id.dakaijiaoR);
        textView3R = findViewById(R.id.bihejiaoR);
        textView4R = findViewById(R.id.duzhichiR);
        textView5R = findViewById(R.id.yucelabelR);
        textView6R = findViewById(R.id.yucescoreR);

        linearLayoutLeft=findViewById(R.id.report_left);
        linearLayoutRight=findViewById(R.id.report_right);

        SharedPreferences sp = getSharedPreferences("my_cache", Context.MODE_PRIVATE);
        conDeviceNum = sp.getInt("conDeviceNum", 1);
        Integer handDir = sp.getInt("handDirection",0);
        if(conDeviceNum==1) {
            linearLayoutRight.setVisibility(View.GONE);
            if (handDir == 1) {
                textViewTitle.setText("左手测试结果");
            }
            if (handDir == 2) {
                textViewTitle.setText("右手测试结果");
            }
        }else{
            textViewTitle.setText("左手测试结果");
        }

        predictLabel = sp.getInt("predictLabel", 100);
        predictScore = sp.getInt("predictScore", 100);
        displayFeaStr = sp.getString("displayFea", "");
        displayFea = displayFeaStr.split(",");
        System.out.println("DuizhiResult----" + displayFea[0] + " " + displayFea[1] + " " + displayFea[2] + " " + displayFea[3]);
        if (displayFea.length == 4) {
            textView1.setText(String.format(Locale.US, "%.2e", Double.parseDouble(displayFea[0])).toString() + "rad/s");
            textView2.setText(String.format(Locale.US, "%.2e", Double.parseDouble(displayFea[1])).toString() + "rad/s");
            textView3.setText(String.format(Locale.US, "%.2e", Double.parseDouble(displayFea[2])).toString() + "rad/s");
            textView4.setText(displayFea[3] + "次");
            textView5.setText(utils.duizhiLabelChange(predictLabel));
            textView6.setText(predictScore.toString());
        } else {
            Toast.makeText(DuizhiResActivity.this, "结果有错误，请重试", Toast.LENGTH_SHORT).show();
        }

        if(conDeviceNum==2){
            linearLayoutRight.setVisibility(View.VISIBLE);
            textViewTitleR.setText("右手测试结果");
            predictLabelR = sp.getInt("predictLabelR", 100);
            predictScoreR = sp.getInt("predictScoreR", 100);
            displayFeaStrR = sp.getString("displayFeaR", "");
            displayFeaR = displayFeaStrR.split(",");
            System.out.println("DuizhiResultR----" + displayFeaR[0] + " " + displayFeaR[1] + " " + displayFeaR[2] + " " + displayFeaR[3]);
            if (displayFea.length == 4) {
                textView1R.setText(String.format(Locale.US, "%.2e", Double.parseDouble(displayFeaR[0])).toString() + "rad/s");
                textView2R.setText(String.format(Locale.US, "%.2e", Double.parseDouble(displayFeaR[1])).toString() + "rad/s");
                textView3R.setText(String.format(Locale.US, "%.2e", Double.parseDouble(displayFeaR[2])).toString() + "rad/s");
                textView4R.setText(displayFeaR[3] + "次");
                textView5R.setText(utils.duizhiLabelChange(predictLabelR));
                textView6R.setText(predictScoreR.toString());
            } else {
                Toast.makeText(DuizhiResActivity.this, "结果有错误，请重试", Toast.LENGTH_SHORT).show();
            }
        }

    }
    // 处理返回箭头点击事件
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish(); // 结束当前Activity，返回上一个
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}